package com.mike.bdb.ui;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mike.bdb.manager.DBManager;
import com.mike.bdb.util.DataCurrent;
import com.mike.bdb.util.ImageManager;
import com.mike.bdb.util.ReflectUtil;
import com.mike.swt.util.ShellUtil;
import com.sleepycat.persist.model.PrimaryKey;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class QueryEntityByKeyDialog {

	private static final Logger LOG = LoggerFactory.getLogger(QueryEntityByKeyDialog.class);
	
	private Shell motherland;
	protected Shell shell;

	private static volatile boolean open = false;

	private Composite	composite;

	private List<Method>	berkeleyQueryAttributeByPK;
	
	/** 方法对象 文本框体 */
	private Map<Method, Text> methodMap = new HashMap<Method, Text>();
	
	/** 方法名 返回值类型 */
	private HashMap<String, Type> methodReturnMap;

	private MainWindow mainWindow;

	private Button	findButton;
	
	public QueryEntityByKeyDialog(Shell motherland, MainWindow mainWindow) {
		this.motherland = motherland;
		this.mainWindow = mainWindow;
	}
	
	public boolean isOpen() {
		return open;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			QueryEntityByKeyDialog window = new QueryEntityByKeyDialog(null, null);
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		
		if(!canOpen()) return;
		
		Display display = Display.getDefault();
		createContents();
		shell.open();
		open = true;
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		open = false;
	}

	private boolean canOpen() {
		boolean db = DataCurrent.getInstance().curDataBasePath != null;
		boolean clazz = DataCurrent.getInstance().curStoreClazz != null;
		boolean result = clazz && db;
		if(result) {
			initData();
		}
		return result;
	}

	private void initData() {
		String curStoreClazz = DataCurrent.getInstance().curStoreClazz;
		Class<Object> classBy = ReflectUtil.getClassBy(curStoreClazz);
		berkeleyQueryAttributeByPK = ReflectUtil.getBerkeleyQueryPKAttribute(classBy);
		if(null == berkeleyQueryAttributeByPK) return;
		
		if(LOG.isDebugEnabled()) {
			LOG.debug("curStoreClazz:" + curStoreClazz);
			LOG.debug("Database Path:" + DataCurrent.getInstance().curDataBasePath);
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.CLOSE | SWT.BORDER | SWT.APPLICATION_MODAL);
		shell.setImage(ImageManager.getImage(ImageManager.ICON_FIND_BY_KEY));
		String curStoreClazz = DataCurrent.getInstance().curStoreClazz;
		String[] split = curStoreClazz.split("\\.");
		String entityName = split[split.length - 1];
		shell.setText(Messages.getString("QueryEntityDialog.title")
				+ " " 
				+ entityName
				+ " [PrimaryKey]"
				); //$NON-NLS-1$
		shell.setSize(480, 200 + 1 * 20);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			{
				{
					composite = new Composite(shell, SWT.NONE);
					composite.setLayout(new GridLayout(3, false));
					
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						
						new Label(composite, SWT.NONE);
						
						dynamicFieldInput();
						
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						new Label(composite, SWT.NONE);
						
						findButton = new Button(composite, SWT.NONE);
						findButton.addSelectionListener(new SelectionAdapter() {
							@Override
							public void widgetSelected(SelectionEvent e) {
								if(null != mainWindow
										&& null != methodMap
										&& null != methodReturnMap) {
									if(methodMap.size() > 0
											&& methodReturnMap.size() > 0) {
										mainWindow.queryPKCondition(methodMap, methodReturnMap);
									}
								}
							}
						});
						{
							GridData gridData_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
							gridData_1.widthHint = 75;
							findButton.setLayoutData(gridData_1);
						}
						findButton.setText(Messages.getString("QueryEntityDialog.button")); //$NON-NLS-1$
						findButton.setImage(ImageManager.getImage(ImageManager.ICON_FIND_BY_KEY));
				}
			}
		}
		
		if (null != motherland) {
			ShellUtil.setCenter(shell, motherland);
		}
	}

	private void dynamicFieldInput() {
		if( null == berkeleyQueryAttributeByPK) return;
		if(null == methodReturnMap) {
			methodReturnMap = new HashMap<String, Type>();
		}
		
		methodMap.clear();
		methodReturnMap.clear();
		
		for (Method method : berkeleyQueryAttributeByPK) {
			
			new Label(composite, SWT.NONE);
			
			Label label_each = new Label(composite, SWT.NONE);
			label_each.setAlignment(SWT.RIGHT);
			{
				GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
				gridData.widthHint = 90;
				label_each.setLayoutData(gridData);
			}
			
			label_each.setText(method.getName().replace("get", "") + "：");
			
			Text text_each = new Text(composite, SWT.BORDER);
			{
				GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
				gridData.widthHint = 300;
				text_each.setLayoutData(gridData);
			}
			
			methodMap.put(method, text_each);
			methodReturnMap.put(method.getName(), method.getGenericReturnType());
		}
	}
	
	
}
